/**
 * 改插件作用将静态第三方资源发布到public目录下
 */

const fs = require('fs');
const path = require('path');

let msgInfo = '';

const print = function(msg) {
  msgInfo = msg;
  return msgInfo;
};
let baseUrl = '../public/';
//  保留不替换的文件
let noChangeFile = [
  // 'pdmConfig/config.js',
  // 'pdmConfig/operationConfig/tableButton.js',
  // 'pdmConfig/operationConfig/hotkeyConfig.js'
];
function deleteFolderRecursive(folderPath) {
  //判断文件夹是否存在
  if (fs.existsSync(folderPath)) {
    //读取文件夹下的文件目录，以数组形式输出
    fs.readdirSync(folderPath).forEach(file => {
      //拼接路径
      const curPath = path.join(folderPath, file); //判断是不是文件夹，如果是，继续递归
      if (fs.lstatSync(curPath).isDirectory()) {
        let filterFlag = noChangeFile.some(item => path.resolve(__dirname, baseUrl + item) === curPath);
        if (!filterFlag) {
          deleteFolderRecursive(curPath);
        }
      } else {
        let filterFlag = noChangeFile.some(item => path.resolve(__dirname, baseUrl + item) === curPath);
        if (!filterFlag) {
          //删除文件或文件夹
          fs.unlinkSync(curPath);
        }
      }
    });
    //仅可用于删除空目录
    try {
      fs.rmdirSync(folderPath);
    } catch (error) {}
  }
}
const copyFileOrDir = function(resourcePath, targetPath, filter = []) {
  // 判断目标文件夹是否存在
  try {
    fs.statSync(targetPath);
  } catch (e) {
    fs.mkdirSync(targetPath, err => {
      print(err);
    });
  }
  fs.readdir(resourcePath, function(err, files) {
    if (err === null) {
      files.forEach(function(fileNameInfo) {
        let filterFlag = filter.some(item => item === fileNameInfo);
        let filedir = path.join(resourcePath, fileNameInfo);
        if (!filterFlag) {
          fs.stat(filedir, function(errs, statsInfo) {
            let isFileInfo = statsInfo.isFile();
            if (isFileInfo) {
              const destPath = path.join(targetPath, fileNameInfo);
              if (!fs.existsSync(destPath)) {
                fs.copyFile(filedir, destPath, err => {
                  print(err);
                });
              }
            } else {
              let tarFiledir = path.join(targetPath, fileNameInfo);
              if (!fs.existsSync(tarFiledir)) {
                fs.mkdirSync(tarFiledir, err => {
                  print(err);
                });
              }
              copyFileOrDir(filedir, tarFiledir, filter);
            }
          });
        }
      });
    } else {
      if (err) {
      }
    }
  });
};
class listplugins {
  apply(compiler) {
    const srcPath = path.resolve(
      __dirname,
      `../node_modules/@ty/plm-p-vue3' : ''}/ty_plm_vue3_sdk`
    );
    let tarPath = path.resolve(__dirname, '../public');
    deleteFolderRecursive(tarPath);
    if (process.env.NODE_ENV !== 'development' && process.env.NODE_ENV !== 'local') {
      compiler.hooks.run.tap('listplugins', compilation => {
        copyFileOrDir(srcPath, tarPath);
      });
    }
    if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'local') {
      compiler.hooks.emit.tapAsync('listplugins', (compilation, callback) => {
        copyFileOrDir(srcPath, tarPath);
        callback();
      });
    }
  }
}
if (process.argv[2] === 'normal') {
  const srcPath = path.resolve(__dirname, `../node_modules/@ty/plm-p-vue3/ty_plm_vue3_sdk`);
  let tarPath = path.resolve(__dirname, '../public');
  deleteFolderRecursive(tarPath);
  copyFileOrDir(srcPath, tarPath);
}
module.exports = listplugins;
